using System;
using System.Collections;
using UnityEngine;

public class PlayerView : MonoBehaviour
{
	public FPSCamera FPSCamera;

	public OrbitCamera FreeCamera;

	public GameObject[] PlayerObjects;

	[NonSerialized]
	public OrbitCamera freeCamera;

	public float OrbitDistance = 10f;

	public PlayerViewType View;

	private CharacterSystem character;

	public void OnPlayerAuthorized()
	{
		if (freeCamera == null)
		{
			freeCamera = UnityEngine.Object.Instantiate(FreeCamera, base.transform.position, Quaternion.identity);
			freeCamera.Target = base.transform.root.gameObject;
		}
		ViewUpdate();
	}

	private void Awake()
	{
		character = GetComponent<CharacterSystem>();
		FPSCamera = GetComponentInChildren<FPSCamera>();
	}

	public void SwithView()
	{
		switch (View)
		{
		case PlayerViewType.FirstVeiw:
			View = PlayerViewType.ThirdView;
			break;
		case PlayerViewType.ThirdView:
			View = PlayerViewType.FirstVeiw;
			break;
		}
	}

	public void SwithViewSide()
	{
		FPSCamera.ThirdViewInvert *= -1;
	}

	private void LateUpdate()
	{
		if ((bool)freeCamera)
		{
			freeCamera.gameObject.SetActive(View == PlayerViewType.FreeView);
		}
		ViewUpdate();
		FPSCamera.IsThirdView = (View == PlayerViewType.ThirdView);
		if (character.Motor.MotorPreset.Length > character.MovementIndex)
		{
			FPSCamera.transform.localPosition = character.Motor.MotorPreset[character.MovementIndex].FPSCamOffset;
		}
	}

	public void ViewUpdate()
	{
		if ((bool)character && character.IsMine)
		{
			if (View == PlayerViewType.FirstVeiw)
			{
				hidePlayerObjects(hide: false);
				if ((bool)FPSCamera)
				{
					FPSCamera.gameObject.SetActive(value: true);
				}
				if ((bool)freeCamera)
				{
					freeCamera.gameObject.SetActive(value: false);
				}
			}
			if (View == PlayerViewType.FreeView)
			{
				hidePlayerObjects(hide: true);
				if ((bool)FPSCamera)
				{
					FPSCamera.gameObject.SetActive(value: false);
				}
				if ((bool)freeCamera)
				{
					freeCamera.gameObject.SetActive(value: true);
					freeCamera.distance = OrbitDistance;
				}
			}
			if (View == PlayerViewType.ThirdView)
			{
				if (FPSCamera.zooming && FPSCamera.hideWhenScoping)
				{
					hidePlayerObjects(hide: false);
				}
				else
				{
					hidePlayerObjects(hide: true);
				}
			}
			if (UnitZ.gameManager.GameViewType != GameView.Both)
			{
				if (UnitZ.gameManager.GameViewType == GameView.FPS && View == PlayerViewType.ThirdView)
				{
					View = PlayerViewType.FirstVeiw;
				}
				if (UnitZ.gameManager.GameViewType == GameView.TPS && View == PlayerViewType.FirstVeiw)
				{
					View = PlayerViewType.ThirdView;
				}
			}
		}
		if ((bool)character && !character.IsMine)
		{
			if ((bool)FPSCamera)
			{
				FPSCamera.gameObject.SetActive(value: false);
			}
			if ((bool)freeCamera)
			{
				freeCamera.gameObject.SetActive(value: false);
			}
			hidePlayerObjects(hide: true);
		}
	}

	private void hidePlayerObjects(bool hide)
	{
		GameObject[] playerObjects = PlayerObjects;
		foreach (GameObject gameObject in playerObjects)
		{
			if (gameObject != null)
			{
				gameObject.SetActive(hide);
			}
		}
	}

	public void Hide(Transform trans, bool hide)
	{
		IEnumerator enumerator = trans.GetEnumerator();
		try
		{
			while (enumerator.MoveNext())
			{
				Transform transform = (Transform)enumerator.Current;
				transform.gameObject.SetActive(hide);
			}
		}
		finally
		{
			IDisposable disposable;
			if ((disposable = (enumerator as IDisposable)) != null)
			{
				disposable.Dispose();
			}
		}
		trans.gameObject.SetActive(hide);
	}
}
